package com.msconfig.widget;

/*
 * author: caojunhao
 * date  : 2020/5/14
 * desc  :绘制地图背景--包括三个圆+xy轴+东南西北+下面的指示图
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.geo.msconfig.R;
import com.msconfig.utils.DisplayUtils;


public class DrawEarth extends View {
    private Context context;
    private float mEarthCircleX;
    private float mEarthCircleY;
    private float mEarthRadius;
    private String[] strings={"GPS","GLONASS","BEIDOU","GALILEO"};
    private int[] colors;
    private Paint paint;
    private int mWidth;
    private int mHeight;

    public DrawEarth(Context context, int width, int heigth) {
        this(context,null);
        this.mWidth=width;
        this.mHeight=heigth;
        this.mEarthCircleX = width/2;//540
        this.mEarthCircleY = heigth/2;
        this.mEarthRadius = mEarthCircleX - mEarthCircleX /20;//513
    }

    public DrawEarth(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawEarth(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        colors=new int[4];

        colors[0]= ContextCompat.getColor(context, R.color.gpscolor);
        colors[1]= ContextCompat.getColor(context,R.color.glonasscolor);
        colors[2]= ContextCompat.getColor(context,R.color.beidoucolor);
        colors[3]= ContextCompat.getColor(context,R.color.galileocolor);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Style.STROKE);
        paint.setTextSize(DisplayUtils.dp2px(context,12));
    }


    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int index=0;
        int left,top,right,bottom;
        int width,height = 0;
        String text;

        //绘制3个圆
        canvas.drawCircle(mEarthCircleX, mEarthCircleY, mEarthRadius *3/6, paint);
        canvas.drawCircle(mEarthCircleX, mEarthCircleY, mEarthRadius *5/6, paint);
        canvas.drawCircle(mEarthCircleX, mEarthCircleY, mEarthRadius, paint);

        //绘制线和外围的度数
        for(int i=0;i<12;i++){
            text=index+"°";
            paint.setColor(Color.BLACK);
            canvas.drawLine(mEarthCircleX, mEarthCircleY - mEarthRadius, mEarthCircleX, mEarthCircleY + mEarthRadius, paint);//从Y轴开始绘制线
            paint.setColor(Color.BLUE);
            paint.setStyle(Style.FILL_AND_STROKE);
            canvas.drawText(text, mEarthCircleX -(getTextBounds(paint,text).width()/2), mEarthCircleY - mEarthRadius, paint);//0°开始绘制度数
            index+=30;
            canvas.rotate(30, mEarthCircleX, mEarthCircleY);
        }
        //绘制垂直方向的度数
        text="30°";
        canvas.drawText(text, mEarthCircleX -(getTextBounds(paint,text).width()/2), mEarthCircleY - mEarthRadius *5/6, paint);
        text="60°";
        canvas.drawText(text, mEarthCircleX -(getTextBounds(paint,text).width()/2), mEarthCircleY - mEarthRadius *3/6, paint);

        //绘制最下面的指示图
        //地球下面的高度大于一定距离
//        if(mHeight-(mEarthCircleY + mEarthRadius)>DisplayUtils.dp2px(context,50)){
//            top=(int)(mEarthCircleY + mEarthRadius + DisplayUtils.dp2px(context,50));
//        }else{
//            top=(int)(mEarthCircleY + mEarthRadius + DisplayUtils.dp2px(context,10));
//        }


        width=(int)(mEarthRadius *2/5);
        left=(mWidth-(width*4))/2;
        right=left+width;

        height=(int)(mEarthRadius *1/5);
        top=mHeight-(height+DisplayUtils.dp2px(context,30));
        bottom=top+height;


        for(int i=0;i<4;i++){
            text=strings[i];
            paint.setStyle(Style.FILL);
            paint.setColor(colors[i]);
            Rect rect=new Rect(left,top,right,bottom);
            canvas.drawRect(rect,paint);
            paint.setStyle(Style.FILL_AND_STROKE);
            paint.setColor(Color.BLACK);
            paint.setTextSize(DisplayUtils.dp2px(context,12));
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(text,left+width/2,bottom-height/3,paint);

            left+=width;
            right+=width;
        }
    }

    public Rect getTextBounds(Paint paint, String str){
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect;
    }
}
